package org.jeecg.modules.FinanceModule.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.jeecg.common.api.vo.Result;
import org.jeecg.modules.FinanceModule.entity.RuturnMoneyEntity;
import org.jeecg.modules.FinanceModule.service.ReturnMoneyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/returnMoney")
public class RetrunMoneyController {

    @Autowired
    ReturnMoneyService service;

    /*
    查询退款处理列表
     */
    @GetMapping("/list")
    public Result<?> list(
            @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
            @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
            @RequestParam(name = "status", defaultValue = "10") Integer status,
            String orderSn, //根据订单编号模糊查询
            String companyCode, //根据客户编号模糊查询
            String companyName
    ) {

        Page<RuturnMoneyEntity> page = new Page<RuturnMoneyEntity>(pageNo, pageSize);
        IPage<RuturnMoneyEntity> pageList = service.getReturnMoneylist(page,orderSn,companyCode,status,companyName);
        return Result.ok(pageList);
    }


    /*
    拒绝退款  修改一下退款的状态
     */
    @GetMapping("/refuse")
    public Result<?> refuseApply(
            @RequestParam(name = "id") int id,
            @RequestParam(name = "userId") String userId,
            @RequestParam(name = "money") String money,

            String orderSn

    ) {
        int count = service.refuseApply(id,userId,money,orderSn);
        if (count > 0) {
            return Result.ok("拒绝成功");
        } else {
            return Result.ok("拒绝失败");
        }
    }

    /*
    同意退款
     */
    @GetMapping("/approve")
    public Result<?> approve(
            @RequestParam(name = "id") int id,
            @RequestParam(name = "userId") String userId,
            @RequestParam(name = "money") String money,
            @RequestParam(name = "remark") String remark,
            @RequestParam(name = "orderType") int orderType,
            String orderMoney,
            String cartId,
            String orderSn
    ) {
        try {

            userId=service.getCompanyuserId(userId);


            int count = service.approve(id, userId, money,remark,orderType,cartId,orderMoney,orderSn);
            if (count > 0) {
                return Result.ok("确认成功");
            } else {
                return Result.ok("确认失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return Result.ok("确认出错");
        }

    }

}
